#include "util.hpp"
#include <functional>
#include <vector>
using func_t = std::function<void()>;

void DownLoad()
{
    std::cout << "this is a download" << std::endl;
}
void Print()
{
    std::cout << "this is a print" << std::endl;
}

#define INIT(v) do{ \
        v.push_back(DownLoad);\
        v.push_back(Print);\
    }while(0)

#define EXEC_OTHER(v) do{\
        for(const auto& e : v) e();\
    }while(0)

int main()
{
    std::vector<func_t> cbs;
    INIT(cbs);
    char buffer[1024];
    Util::SetNonBlock(0);
    while(true)
    {
        std::cout << ">>> ";
        fflush(stdout);
        int s = read(0, buffer, sizeof(buffer) - 1);
        if(s > 0)
        {
            buffer[s - 1] = 0;
            std::cout << "echo # " << buffer << std::endl;
        }
        else if(s == 0)
        {
            std::cout << "read file end " << std::endl;
        }
        else
        {
            //todo
            if(errno == EAGAIN || errno == EWOULDBLOCK)
            {
                std::cout << "time out..." << std::endl;
                EXEC_OTHER(cbs);
            }
            else if(errno == EINTR)
            {
                std::cout << "" << std::endl;
            }
            else
            {
                std::cout << "errno: " << errno << " errno string: " << strerror(errno) << std::endl;
            }
        }
        sleep(1);
    }
    return 0;
}